package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.LineDispatchPsnMapper;
import com.stylefeng.guns.modular.netcar.model.LineDispatchPsn;
import com.stylefeng.guns.modular.netcar.service.ILineDispatchPsnService;
import com.stylefeng.guns.modular.system.model.Org;
import com.stylefeng.guns.modular.system.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 线路指派调度员 Service 实现 
 * @author 张亚林
 * @Date 2018-10-27 09:52
 */
@Service
public class LineDispatchPsnServiceImpl extends ServiceImpl<LineDispatchPsnMapper,LineDispatchPsn> implements ILineDispatchPsnService {

    @Autowired
    private IOrgService orgService;
    @Resource
    private LineDispatchPsnMapper lineDispatchPsnMapper;

    /**
     * 删除线路指派调度员
     * @param lineDispatchPsn
     * @return     
     */
    @Transactional
    public int deleteLineDispatchPsn(LineDispatchPsn lineDispatchPsn,String token)throws Exception{
        if(lineDispatchPsn==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.lineDispatchPsnMapper.deleteLineDispatchPsn(lineDispatchPsn);
    }
    /**
     * 批量删除线路指派调度员
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchLineDispatchPsn(String[] ids,String token)throws Exception{
        return this.lineDispatchPsnMapper.deleteBatchLineDispatchPsn(ids);
    }
    /**
     * 查询线路指派调度员列表
     * @param lineDispatchPsn 线路指派调度员对象
     * @return
     */
    public List<LineDispatchPsn> selectListLineDispatchPsn(LineDispatchPsn lineDispatchPsn,String token)throws Exception{
        return  this.lineDispatchPsnMapper.selectListLineDispatchPsn(lineDispatchPsn);
    }
    /**
	 * 分页查询线路指派调度员列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListLineDispatchPsn(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(lineDispatchPsnMapper.selectListLineDispatchPsn(page,(LineDispatchPsn)pager.getObj()));
        List<LineDispatchPsn> list = page.getRecords();
        Org selOrg=new Org();
        selOrg.setSoType("1");
        List<Org> listOrg=orgService.selectListOrg(selOrg);
        for(LineDispatchPsn psn:list){
            String soPath=psn.getSoPath();
            String[] pidArry=soPath.replace("[","").replaceAll("]","").split(",");
            boolean existsFlg=false;
            for(int i=pidArry.length;i>0;i--){
                String pid=pidArry[i-1];
                if(pid.length()==0){
                    continue;
                }
                for(Org org:listOrg){
                    if(pid.equals(org.getSoId().toString())){
                        psn.setSuName(org.getSoName()+"-"+psn.getRealSuName());
                        existsFlg=true;
                        break;
                    }
                }
                if(existsFlg){
                    break;
                }
            }
        }
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param lineDispatchPsn 线路指派调度员对象
     * @return 
     */
    public  List<LineDispatchPsn> selectListLineDispatchPsnForUpdate(LineDispatchPsn lineDispatchPsn)throws Exception{
        return  this.lineDispatchPsnMapper.selectListLineDispatchPsnForUpdate(lineDispatchPsn);
    }
    /**
     * 通过主键id查询的线路指派调度员列表
     * @param id 主键id
     * @return     
     */
    public  LineDispatchPsn findLineDispatchPsnById(String id,String token)throws Exception{
        LineDispatchPsn dispatchPsn=  this.lineDispatchPsnMapper.findLineDispatchPsnById(id);
        Org selOrg=new Org();
        selOrg.setSoType("1");
        List<Org> listOrg=orgService.selectListOrg(selOrg);

        String soPath=dispatchPsn.getSoPath();
        String[] pidArry=soPath.replace("[","").replace("]","").split(",");
        boolean existsFlg=false;
        for(int i=pidArry.length;i>0;i--){
            String pid=pidArry[i-1];
            if(pid.length()==0){
                continue;
            }
            for(Org org:listOrg){
                if(pid.equals(org.getSoId().toString())){
                    dispatchPsn.setSuName(org.getSoName()+"-"+dispatchPsn.getRealSuName());
                    existsFlg=true;
                    break;
                }
            }
            if(existsFlg){
                break;
            }
        }
        return dispatchPsn;
    }
    /**
     * 新增线路指派调度员数据
     * @param lineDispatchPsn  线路指派调度员对象
     * @return
     */
    @Transactional
    public int insertLineDispatchPsn(LineDispatchPsn lineDispatchPsn,String token) throws Exception { 
        if(lineDispatchPsn!=null && (lineDispatchPsn.getLdpId()==null || lineDispatchPsn.getLdpId().trim().length()<10)){
            lineDispatchPsn.setLdpId(IdWorker.get32UUID());
        }  
        return this.lineDispatchPsnMapper.insertLineDispatchPsn(lineDispatchPsn);
    }
    
    /**
     * 更新线路指派调度员数据
     * @param lineDispatchPsn 线路指派调度员对象
     * @return     
     */
    @Transactional
    public int updateLineDispatchPsn(LineDispatchPsn lineDispatchPsn,String token) throws Exception {     	
        return  this.lineDispatchPsnMapper.updateLineDispatchPsn(lineDispatchPsn);
    }    
    /**
     * 查询线路指派调度员返回key-Value属性
     * @param lineDispatchPsn 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsLineDispatchPsn(LineDispatchPsn lineDispatchPsn,String token) throws Exception {
        List<LineDispatchPsn> listLineDispatchPsn=this.lineDispatchPsnMapper.selectListLineDispatchPsn(lineDispatchPsn);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(LineDispatchPsn item:listLineDispatchPsn){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getLdpId());
            opts.setValue(item.getOlId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
